package Test

import (
	"Volta/Core/Facade"
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
)

type TestCronJob struct {
	IntervalTime int64  //.7200s
}

//.定时任务逻辑写在函数内
func (app *TestCronJob) Handle() {
	Create()
	list(1,10)
}

func Create(){
	ESConn := Facade.Accessor.GoElastic.GetElastic()
	type Employee struct {
		Name      string   `json:"name"`
		Sex       int      `json:"sex"`
		Age       int      `json:"age"`
		About     string   `json:"about"`
		Interests []string `json:"interests"`
	}
	//使用结构体
	e1 := Employee{"晋辉辉", 1, 27, "辉辉天天1玩自己的dick", []string{"play dick every day"}}
	_, err := ESConn.Index().
		Index("megacorp").
		Type("employee").
		BodyJson(e1).
		Do(context.Background())
	if err != nil {
		fmt.Println("create err")
	}
}


func Get() {
	ESConn := Facade.Accessor.GoElastic.GetElastic()
	//通过id查找:
	get1, err := ESConn.Get().Index("megacorp").Type("employee").Id("1").Do(context.Background())
	if err != nil {
		fmt.Println(err.Error())
	}
	//var res map[string]interface{}
	if get1.Found {
		fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
		jsonStr,_ := get1.Source.MarshalJSON()
		fmt.Println(string(jsonStr))
	}
}

func Search(){
	ESConn := Facade.Accessor.GoElastic.GetElastic()
	//取所有
	var res *elastic.SearchResult
	var err error
	q := elastic.NewQueryStringQuery("name:王子康")
	res, err = ESConn.Search().Index("megacorp").Type("employee").Query(q).Do(context.Background())
	if err != nil {
		fmt.Println(err.Error())
		return
	}


	//条件查询
	//年龄大于30岁的
	boolQ := elastic.NewBoolQuery()
	//boolQ.Must(elastic.NewMatchQuery("name", "smith"))
	boolQ.Filter(elastic.NewRangeQuery("age").Gt(10))
	res, err = ESConn.Search("megacorp").Type("employee").Query(boolQ).Do(context.Background())
	if res.TotalHits() > 0{
		for _,hit := range res.Hits.Hits{
			jsonStr,_ := hit.Source.MarshalJSON()
			fmt.Println(string(jsonStr))
		}
	}

	//短语搜索 搜索about字段中有 rock climbing
	matchPhraseQuery := elastic.NewMatchPhraseQuery("about", "fuck")
	res, err = ESConn.Search("megacorp").Type("employee").Query(matchPhraseQuery).Do(context.Background())
	if res.TotalHits() > 0{
		for _,hit := range res.Hits.Hits{
			jsonStr,_ := hit.Source.MarshalJSON()
			fmt.Println(string(jsonStr))
		}
	}
	//分析 interests
	aggs := elastic.NewTermsAggregation().Field("music")
	res, err = ESConn.Search("megacorp").Type("employee").Aggregation("interests", aggs).Do(context.Background())
	if res.TotalHits() > 0{
		for _,hit := range res.Hits.Hits{
			jsonStr,_ := hit.Source.MarshalJSON()
			fmt.Println(string(jsonStr))
		}
	}
}

//简单分页
func list(page,size int) {
	ESConn := Facade.Accessor.GoElastic.GetElastic()
	if size < 0 || page < 1 {
		fmt.Printf("param error")
		return
	}
	res, err := ESConn.Search("megacorp").
		Type("employee").
		Size(size).
		From((page - 1) * size).
		Do(context.Background())
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	if res.TotalHits() > 0{
		for _,hit := range res.Hits.Hits{
			jsonStr,_ := hit.Source.MarshalJSON()
			fmt.Println(string(jsonStr))
		}
	}
}